package com.programmercarl.greed;

/**
 * https://leetcode.cn/problems/lemonade-change/description/
 * 柠檬水找零    AC
 *
 * @author yulongtian
 * @create 2025-02-07 11:25
 */
public class Test09 {
    public static void main(String[] args) {
        Test09 test09 = new Test09();
//        System.out.println(test09.lemonadeChange(new int[]{5,5,10,10,20}));
        System.out.println(test09.lemonadeChange(new int[]{10}));
    }

    public boolean lemonadeChange(int[] bills) {
        int n = bills.length;
        int[] money = new int[3];
        for (int i = 0; i < n; i++) {
            boolean check = check(money, bills[i]);
            if (!check) {
                return false;
            }
        }

        return true;
    }

    public boolean check(int[] money, int pay) {
        if (pay == 5) {
            money[0]++;
            return true;
        }
        if (pay == 10) {
            if (money[0] > 0) {
                money[0]--;
                money[1]++;
                return true;
            }
        }
        if (pay == 20) {
            // 10+5 或5+5+5
            if (money[1] > 0 && money[0] > 0) {
                money[0]--;
                money[1]--;
                money[2]++;
                return true;
            } else if (money[0] > 2) {
                money[0] -= 3;
                money[2]++;
                return true;
            }
        }
        return false;
    }

}
